home *** CD-ROM | disk | FTP | other *** search
- /* some functions to respond to various user inputs...
- * 870806-13-... ^z
- */
-
- #include <stdio.h> /* for FILE, printf(), etc. */
- #include <strings.h> /* for strcpy(), etc. */
- #include <unix.h> /* for exit(), etc. */
- #include <proto.h> /* for function prototypes */
- #include "brwsr.h" /* for various definitions */
- #include "brwsr.proto.h" /* for my function prototypes */
-
-
- /* function to move down a level in the browser hierarchy:
- * INDEX --> CONTEXT --> TEXT
- * includes various safety features against errors, and takes
- * care of initializations for the level being entered ... also
- * displays the current line when entering the new level, for the user's
- * convenience....
- *
- * Modified for subset operations, ^z - 870813. Note that we must check
- * here to be sure that we are descending into a non-empty subset when
- * a subset is active and we are descending from INDEX --> CONTEXT;
- * we also must make sure in that case that we initialize the CONTEXT
- * display to start with the first good item, not just min_item[CONTEXT].
- */
-
- void do_descend ()
- {
- extern int level, empty_subset;
- extern long current_item[], min_item[], max_item[], subset_rec_count;
- extern KEY_REC this_rec, prev_rec;
- extern FILE *doc_file;
- extern char *subset;
-
- if (doc_file == NULL)
- {
- beep ();
- printf ("No file open!\n");
- return;
- }
-
- if (current_item[INDEX] < 0)
- {
- beep ();
- printf ("No current index item!\n");
- return;
- }
-
- if (subset != NULL && (empty_subset || subset_rec_count == 0))
- {
- beep ();
- printf ("No items in subset!\n");
- return;
- }
-
- ++level;
-
- if (level == CONTEXT)
- {
- min_item[CONTEXT] = prev_rec.ccount;
- max_item[CONTEXT] = this_rec.ccount - 1;
- if (subset == NULL)
- current_item[CONTEXT] = min_item[CONTEXT];
- else
- {
- current_item[CONTEXT] = min_item[CONTEXT] - 1;
- make_subindex_move (1L);
- }
- current_item[TEXT] = get_ptr_rec (current_item[CONTEXT]);
- }
-
- if (level == TEXT)
- current_item[TEXT] =
- start_of_line (current_item[TEXT]);
-
- if (level > TEXT)
- {
- beep ();
- printf ("Can't descend past full text display!\n");
- level = TEXT;
- }
-
- show_current_item ();
- }
-
- /* function to move up a level in the browser hierarchy:
- * TEXT --> CONTEXT --> INDEX
- * includes a safety net against going past INDEX, and
- * displays the current line when entering the new level...
- * note that when coming up from TEXT, must reset the current_item[TEXT]
- * before displaying CONTEXT line, as TEXT browsing changes it....
- */
-
-
- void do_ascend ()
- {
- extern int level;
-
- --level;
-
- if (level < INDEX)
- {
- beep ();
- printf ("Can't ascend past index display!\n");
- level = INDEX;
- }
-
- if (level == CONTEXT)
- current_item[TEXT] = get_ptr_rec (current_item[CONTEXT]);
-
- show_current_item ();
- }
-
-
- /* function to jump the INDEX display to a target entered by the
- * user ... does a simple binary search (see K&R p.54) to get there
- * and then shows that line of the INDEX ... and if the
- * target word is not found, it beeps and shows the preceeding
- * word available in the index ... if called from another level, it
- * jumps to the INDEX level ...
- */
-
- void do_target_jump (cmd)
- char cmd[];
- {
- register int c, diff;
- register long low, high, mid;
- KEY_REC target_item, this_item;
- extern long current_item[], max_item[];
- extern FILE *key_file;
- extern int level;
-
- if (key_file == NULL)
- {
- beep ();
- printf ("No file open!\n");
- return;
- }
-
- level = INDEX;
- init_target_item (&target_item, cmd);
- low = min_item[INDEX];
- high = max_item[INDEX];
-
- while (low <= high)
- {
- mid = (low + high) / 2;
- get_key_rec (&this_item, mid);
- diff = zstrcmp (target_item.kkey, this_item.kkey);
- if (diff < 0)
- high = mid - 1;
- else if (diff > 0)
- low = mid + 1;
- else
- break;
- }
-
- current_item[INDEX] = mid;
- if (diff < 0)
- --current_item[INDEX];
- if (current_item[INDEX] < 0)
- current_item[INDEX] = 0;
- if (diff != 0)
- beep ();
- show_current_item ();
- }
-
-
-
- /* my function to compare two strings and give a result as to who is
- * alphabetically earlier. Note that this is almost the same as strncmp()
- * with the fixed value of KEY_LENGTH as the maximum comparison distance,
- * except that I must be sure to mask the characters to make them positive
- * (since we want to be able to handle the non-ASCII funny letters in
- * the Apple character set properly/consistently). If the masking isn't
- * done, then inconsistent results can occur with those non-ASCII chars!
- */
-
- int zstrcmp (s1, s2)
- register char *s1, *s2;
- {
- register int n = KEY_LENGTH;
-
- for (; --n && ((*s1 & 0xFF) == (*s2 & 0xFF)); s1++, s2++)
- if (!*s1) break;
-
- return ((*s1 & 0xFF) - (*s2 & 0xFF));
- }
-
-
-
- /* initialize the target KEY_REC kkey string to the value typed in by
- * the user ... convert the user's string to all capital letters, and
- * pad it out to KEY_LENGTH with trailing blanks as needed ... use the
- * toupper() function warily (assume that it may be nonstandard and
- * mess up the value of non-lowercase characters, as it seems to on
- * the VAX and Sun C compilers I've tried -- so check and don't apply
- * toupper() except to lower-case characters!)....
- */
-
- void init_target_item (rp, cmd)
- KEY_REC *rp;
- char cmd[];
- {
- register int c, n;
-
- strncpy (rp->kkey, " ", KEY_LENGTH);
- for (n = 0; n < KEY_LENGTH && (c = cmd[n]) != 0; ++n)
- rp->kkey[n] = (islower (c) ? toupper (c) : c);
- }
-
-
- /* handle the printing out of N lines due to a ".N" user command ... do
- * the job simply by marching down a step and displaying that line N times
- * ... precisely equivalent to hitting the <return> key N times.
- */
-
- void do_multiprint (cmd)
- char cmd[];
- {
- register long i, n;
- extern FILE *doc_file;
-
- if (doc_file == NULL)
- {
- beep ();
- printf ("No file open!\n");
- return;
- }
-
- if (cmd[1] == '\0')
- strcat (cmd, "1");
- n = atol (cmd + 1);
- for (i = 0; i < n; ++i)
- {
- if (! make_move (1L))
- break;
- show_current_item ();
- }
- }
-
-
-